<?php

require_once "config.inc";
require_once "db.inc";

db_connect();

function abstract_array_query($tablename, $custom_block = '') {
    $result = db_query( "SELECT * FROM $tablename" . " $custom_block" );

    while ($row = mysql_fetch_assoc($result)) {
        $rows[] = $row;
    }

    mysql_free_result ($result);
    return $rows;
}


function abstract_insert($table, $data) {
    if ($table && $data) {
        $keys_list   = array();
        $values_list = array();

        while (list($key, $val) = each($data)) {
            $keys_list[]    = $key;
            $values_list[]  = $val;
            $formats_list[] = "'%s'";
        }

        $result =
            db_query("REPLACE INTO `$table` (" . join(',', $keys_list) .
                ") VALUES(" . join (',', $formats_list) . ");",
                $values_list
            );

        return $result;
    } else {
        return '';
    }
}

function abstract_update($table, $field, $value, $where_block) {
    return db_query("UPDATE `$table` SET `$field`='$value'" . " $where_block");
}

/* get data from db */
function get_managers() {
    return
        abstract_array_query('ssau_event_managers');
}

function get_places() {
    return
        abstract_array_query('ssau_event_places');
}

function get_addresses() {
    return
        abstract_array_query('ssau_event_addresses');
}

function get_uuid() {
    $result = db_query("SELECT MD5(UUID()) AS uniq_id");
  
    if ($result) {
      $uuid_data = mysql_fetch_assoc($result);
      return $uuid_data['uniq_id'];
    } else {
        return '';
    }
}

function get_events($event_id = '') {
    if ($event_id) {
        $result = db_query( "SELECT * FROM ssau_events where id='$event_id'" );

        return mysql_fetch_assoc($result);
    } else {
        return abstract_array_query('ssau_events', " WHERE IF(publish_start, publish_start > curdate(), true) AND". 
            " IF(publish_end, curdate() > publish_end, true) " . 
            " ORDER by IF(curdate() < priority_expiration_date, priority, 1) DESC, start_date DESC LIMIT 20");
    }
}
# TODO expiration !!!!
function get_images($event_id) {
    if ($event_id) {
        return
            abstract_array_query("ssau_event_images", "WHERE event_id=$event_id" );
    } else {
        return '';
    }
}


/* add data into db */

function add_event($data) {
    if ($data[name]         &&
        $data[manager]      &&
        $data[place]        &&
        $data[start_date]
    ) {
        $result = abstract_insert('ssau_events', $data);

        if ($result) {
            return $result;
        } else {
            return '';
        }
    } else {
        return '';
    }
}

function add_image($image_name, $event_id) {
    if ($image_name && $event_id) {
        $data[event_id] = $event_id;
        $data[name]     = $image_name;

        return abstract_insert('ssau_event_images', $data);
    } else {
        return '';
    }
}

function add_new_manager($manager_name) {
    $data[name] = $manager_name;
    return abstract_insert('ssau_event_managers', $data);
}

function add_new_place($place_name) {
    $data[name] = $place_name;
    return abstract_insert('ssau_event_places', $data);
}

function add_new_address($place_name) {
    $data[title] = $place_name;
    return abstract_insert('ssau_event_addresses', $data);
}


/* edit data in db */

function set_event_complete($results, $event_id) {
    return abstract_update('ssau_events', 'results', $results, "WHERE id='$event_id'" );
}


function auth($login, $password) {
    $params = array ($password, $login);
    $result = db_query("SELECT MD5('%s')=`password` AS auth_result FROM ssau_events_users WHERE login='%s'", $params );

    if ($result) {
        $auth_result = mysql_fetch_assoc($result);
        return $auth_result["auth_result"];
    } else {
        return ''; # EPIC FAIL!
    }
}

?>
